{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "300c32e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7e0ea46c",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('data/sample_data.csv', index_col=0)[['state', 'food']].iloc[:4]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc3dbabe",
   "metadata": {},
   "source": [
    "# Trying to reverse a pandas columns?\n",
    "\n",
    "* It won't work due to automatic alignment of the index\n",
    "* Use `.values` to get a numpy array, which as no index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f7d17c29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>state</th>\n",
       "      <th>food</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Jane</th>\n",
       "      <td>NY</td>\n",
       "      <td>Steak</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Niko</th>\n",
       "      <td>TX</td>\n",
       "      <td>Lamb</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Aaron</th>\n",
       "      <td>FL</td>\n",
       "      <td>Mango</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Penelope</th>\n",
       "      <td>AL</td>\n",
       "      <td>Apple</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         state   food\n",
       "Jane        NY  Steak\n",
       "Niko        TX   Lamb\n",
       "Aaron       FL  Mango\n",
       "Penelope    AL  Apple"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54631a3b",
   "metadata": {},
   "source": [
    "## Doesn't work due to automatic alignment of the index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d4123c14",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>state</th>\n",
       "      <th>food</th>\n",
       "      <th>food_reverse</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Jane</th>\n",
       "      <td>NY</td>\n",
       "      <td>Steak</td>\n",
       "      <td>Steak</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Niko</th>\n",
       "      <td>TX</td>\n",
       "      <td>Lamb</td>\n",
       "      <td>Lamb</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Aaron</th>\n",
       "      <td>FL</td>\n",
       "      <td>Mango</td>\n",
       "      <td>Mango</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Penelope</th>\n",
       "      <td>AL</td>\n",
       "      <td>Apple</td>\n",
       "      <td>Apple</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         state   food food_reverse\n",
       "Jane        NY  Steak        Steak\n",
       "Niko        TX   Lamb         Lamb\n",
       "Aaron       FL  Mango        Mango\n",
       "Penelope    AL  Apple        Apple"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['food_reverse'] = df['food'][::-1]\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a9563a6",
   "metadata": {},
   "source": [
    "## Extract numpy array, which has no index!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ed5cf34e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>state</th>\n",
       "      <th>food</th>\n",
       "      <th>food_reverse</th>\n",
       "      <th>food_reverse2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Jane</th>\n",
       "      <td>NY</td>\n",
       "      <td>Steak</td>\n",
       "      <td>Steak</td>\n",
       "      <td>Apple</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Niko</th>\n",
       "      <td>TX</td>\n",
       "      <td>Lamb</td>\n",
       "      <td>Lamb</td>\n",
       "      <td>Mango</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Aaron</th>\n",
       "      <td>FL</td>\n",
       "      <td>Mango</td>\n",
       "      <td>Mango</td>\n",
       "      <td>Lamb</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Penelope</th>\n",
       "      <td>AL</td>\n",
       "      <td>Apple</td>\n",
       "      <td>Apple</td>\n",
       "      <td>Steak</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         state   food food_reverse food_reverse2\n",
       "Jane        NY  Steak        Steak         Apple\n",
       "Niko        TX   Lamb         Lamb         Mango\n",
       "Aaron       FL  Mango        Mango          Lamb\n",
       "Penelope    AL  Apple        Apple         Steak"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['food_reverse2'] = df['food'].values[::-1]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01a2be9f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
